/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mobiwebinc.compconn.gui;

import com.mobiwebinc.compconn.Main;
import com.mobiwebinc.compconn.client.ClientList;
import com.mobiwebinc.compconn.configuration.Constant;
import com.mobiwebinc.compconn.events.command.Command;
import java.awt.MenuItem;
import java.awt.MenuShortcut;
import java.awt.PopupMenu;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.JMenu;
import javax.swing.JMenuItem;

/**
 *
 * @author suraj
 */
public class MainMenu implements ActionListener {

    private PopupMenu popupMenu;
    private JMenu menu;
    private static MainMenu mnu;
    JMenuItem startItem, restartItem, stopItem;
    MenuItem start, stop, restart, quit, pref, about;

    private MainMenu(boolean fullMenu) {
        setPopupMenu(new PopupMenu());
//        int shortcutKeyMask = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
        start = new MenuItem(Command.MENU_START, new MenuShortcut(KeyEvent.VK_S));
        start.addActionListener(this);
        stop = new MenuItem(Command.MENU_STOP, new MenuShortcut(KeyEvent.VK_T));
        stop.addActionListener(this);
        restart = new MenuItem(Command.MENU_RESTART, new MenuShortcut(KeyEvent.VK_R));
        restart.addActionListener(this);
        pref = new MenuItem(Command.MENU_PREFERENCES, new MenuShortcut(KeyEvent.VK_P));
        pref.addActionListener(this);
        about = new MenuItem(Command.MENU_ABOUT, new MenuShortcut(KeyEvent.VK_A));
        about.addActionListener(this);
        quit = new MenuItem(Command.MENU_QUIT, new MenuShortcut(KeyEvent.VK_Q));
        quit.addActionListener(this);
        ClientList clientList = ClientList.getInstance();
        ConfigurationGUI configGUI = ConfigurationGUI.getInstance();
        getPopupMenu().add(clientList.getMenu());
        getPopupMenu().add(start);
        getPopupMenu().add(stop);
        getPopupMenu().add(restart);
        getPopupMenu().add(pref);
        if (fullMenu) {
            getPopupMenu().add(about);
            getPopupMenu().add(quit);
        }
        configGUI.add(getPopupMenu());
        menu = new JMenu(Constant.MAIN_MENU_TITLE);
        startItem = new JMenuItem(Command.MENU_START);
        startItem.addActionListener(this);
        stopItem = new JMenuItem(Command.MENU_STOP);
        stopItem.addActionListener(this);
        restartItem = new JMenuItem(Command.MENU_RESTART);
        restartItem.addActionListener(this);
        menu.add(startItem);
        menu.add(stopItem);
        menu.add(restartItem);
        menu.add(clientList.getJMenu());
    }

    public static MainMenu getMainMenu() {
        if (mnu == null) {
            mnu = new MainMenu(false);
        }
        return mnu;
    }

    public static MainMenu getFullMenu() {
        if (mnu == null) {
            mnu = new MainMenu(true);
        }
        return mnu;
    }

    /**
     * @return the popupMenu
     */
    public PopupMenu getPopupMenu() {
        return popupMenu;
    }

    /**
     * @param popupMenu the popupMenu to set
     */
    public void setPopupMenu(PopupMenu menu) {
        this.popupMenu = menu;
    }

    public void actionPerformed(ActionEvent e) {
        String cmd = e.getActionCommand();
        if (cmd.equals(Command.MENU_STOP)) {
            Main.getInstance().stop();
        } else if (cmd.equals(Command.MENU_START)) {
            Main.getInstance().start();
        } else if (cmd.equals(Command.MENU_RESTART)) {
            Main.getInstance().restart();
        } else if (cmd.equals(Command.MENU_PREFERENCES)) {
            ConfigurationGUI.getInstance().showDialog();
        } else if (cmd.equals(Command.MENU_ABOUT)) {
            About.showDialog();
        } else if (cmd.equals(Command.MENU_QUIT)) {
            System.exit(0);
        }
    }

    /**
     * @return the menu
     */
    public JMenu getMenu() {
        return menu;
    }

    /**
     * @param menu the menu to set
     */
    public void setMenu(JMenu menu) {
        this.menu = menu;
    }

    public void setStatus(boolean started) {
        start.setEnabled(!started);
        stop.setEnabled(started);
        startItem.setEnabled(!started);
        stopItem.setEnabled(started);
    }

    public void addClient(String client) {
    }

    public void removeClient(int position) {
    }
}
